package com.base.cn.platform.os.service.classes.topic.like;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.dao.classes.topic.like.ClassesTopicLikeDao;
import com.base.cn.platform.os.entity.classes.topic.ClassesTopic;
import com.base.cn.platform.os.entity.classes.topic.like.ClassesTopicLike;
import com.base.cn.platform.os.service.classes.topic.ClassesTopicBiz;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级话题点赞
 *
 * @author SK
 * @since 2018-05-30
 */
@Service
public class ClassesTopicLikeBiz extends BaseBiz<ClassesTopicLike, ClassesTopicLikeDao> {

    @Autowired
    private ClassesTopicBiz classesTopicBiz;
    @Autowired
    private CusUserService cusUserService;

    public List<ClassesTopicLike> findClassesTopicLikeList(ClassesTopicLike like, Integer count,
                                                           boolean setCusUser) {
        String where = this.where(like);
        List<ClassesTopicLike> likeList = find(where, count, null);
        setCusUser(likeList, setCusUser);
        return likeList;
    }

    /**
     * 保存话题点赞
     *
     * @param topicLike 话题点赞
     */
    public Map<String, Object> saveClassesTopicLike(ClassesTopicLike topicLike) {
        ClassesTopic topic = classesTopicBiz.findClassesTopicById(topicLike.getTopicId(),false,false,false,false);
        if(ObjectUtils.isEmpty(topic)){
            ResultUtil.ERROR("系统繁忙，请稍后重试");
        }
        String where = this.where(topicLike);
        ClassesTopicLike classesTopicLike = this.findOne(where,null);
        String message = "";
        Map<String,Object> likeMap = new HashMap<>();
        if(ObjectUtils.isEmpty(classesTopicLike)){
            this.save(topicLike);
            message = "点赞成功";
            likeMap.put("likeFlag",true);
            classesTopicBiz.updateClassesTopicStatistics(topic, ClassesTopicBiz.LIKE_NUM, 1);
            Map<BigDecimal, Map<String, Object>> cusUserMap = cusUserService.queryCusUserMapByUserIds(topicLike.getUserId().toString(),false, false, false, false, false, false, false);
            likeMap.put("avatar",cusUserMap.get(topicLike.getUserId()).get("avatar"));
        }else{
            this.deleteById(classesTopicLike.getId());
            message = "取消点赞成功";
            likeMap.put("likeFlag",false);
            classesTopicBiz.updateClassesTopicStatistics(topic, ClassesTopicBiz.LIKE_NUM, -1);
        }
        likeMap.put("userId",topicLike.getUserId());
        likeMap.put("likeNum",topic.getLikeNum());
        return ResultUtil.SUCCESS(message,likeMap);
    }

    private void setCusUser(List<ClassesTopicLike> likeList, boolean setCusUser) {
        if (setCusUser && ObjectUtils.isNotEmpty(likeList)) {
            String cusUserIds = DataUtil.joining(likeList, ClassesTopicLike::getUserId);
            Map<BigDecimal, Map<String, Object>> cusUserMap = cusUserService.queryCusUserMapByUserIds(cusUserIds, false, false, false, false, false, false, false);
            likeList.forEach(e -> {
                Map<String,Object> user = cusUserMap.get(e.getUserId());
                e.setCreateUser(user);
            });
        }
    }

    private String where(ClassesTopicLike like) {
        String where = " 1=1";
        if (DataUtil.idIsNotNull(like.getTopicId())) {
            where += " and topicId = " + like.getTopicId();
        }
        if (DataUtil.idIsNotNull(like.getUserId())) {
            where += " and userId = " + like.getUserId();
        }
        return where;
    }
}
